package com.itheima.mongoUtil;

import com.itheima.tanhua.pojo.mongo.Friend;
import com.itheima.tanhua.pojo.mongo.MovementTimeLine;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 芝士雪豹
 * @create 2022/9/24/21:25
 */
@Component   //声明这是一个组件
public class MovementTimeLineService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Async //异步处理
    public void  movementTimeLineService(Long userId, ObjectId movementId){
        //查询当前用户的好友数据
        Query query= Query.query(Criteria.where("userId").is(userId));
        List<Friend> friends = mongoTemplate.find(query, Friend.class);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //循环好友数据,构建时间数据存入数据库
        for (Friend friend : friends) {
            MovementTimeLine timeLine = new MovementTimeLine();
            timeLine.setCreated(System.currentTimeMillis());
            timeLine.setMovementId(movementId);
            timeLine.setFriendId(friend.getFriendId());
            timeLine.setUserId(friend.getUserId());
            mongoTemplate.save(timeLine);
        }
    }



}
